<?php
namespace App\Lib;

use Upyun\Config;
use Upyun\Signature;
use Upyun\Util;

class ImageUploadHandler
{
	protected $allowed_ext = ["png", "jpg", "gif", 'jpeg'];

	/**
	 * 上传图片接口(上传到cdn)
	 * @param  [string]  $folder      [自定义的存放的路径]
	 * @param  [string]  $file_prefix [前缀]
	 * @return [type]               [description]
	 */
	public function saveCdnImage($folder='', $file_prefix=0)
	{
		// 构建存储的文件夹规则，值如：uploads/images/avatars/201709/21/
        // 文件夹切割能让查找效率更高。
        $folder_name = "upfile/ueditor/image/$folder/" . date("Ym/d", time());

        // 拼接文件名，加前缀是为了增加辨析度，前缀可以是相关数据模型的 ID
        $filename = $file_prefix . '_' . time() . '_' . str_random(10).'{.suffix}';

        //拼接上传路径
        $upload_path = $folder_name.$filename;

        $bucket = config('app.cdn_bucket');
        $config = new Config($bucket, 'phpteam', 'phpteam123456');
        $config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw=');
        $data['save-key']             = $upload_path;
        $data['expiration']           = time() + 120;
        $data['bucket']               = $bucket;
        $data['notify-url']           = 'https://hsshop.huisou.cn/merchants/myfile/notify/';
        $data['content-length-range'] = '0,10240000';
        $policy                       = Util::base64Json($data);
        $method                       = 'POST';
        $uri                          = '/' . $bucket;
        $signature                    = Signature::getBodySignature($config, $method, $uri, null, $policy);
        $data                         = [
            'policy'        => $policy,
            'authorization' => $signature,
            'url'           => 'https://v0.api.upyun.com/' . $bucket,
        ];

        return $data;
	}

	
}